﻿using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using Microsoft.Web.Mvc.Internal;

namespace Netkk.Core.Validation
{
    public class ModelStateWrapper : IValidationDictionary
    {

        private readonly ModelStateDictionary modelState;

        public ModelStateWrapper(ModelStateDictionary modelState)
        {
            this.modelState = modelState;
        }

        public void AddError(
            string key, 
            string errorMessage)
        {
            modelState.AddModelError(key, errorMessage);
        }

        public void AddError<TModel, TProperty>(
            Expression<Func<TModel, TProperty>> expression, 
            string errorMessage) where TModel : class
        {
            string inputName = ExpressionHelper.GetInputName(expression);
            AddError(inputName, errorMessage);
        }

        public bool IsValid
        {
            get { return modelState.IsValid; }
        }

    }
}